#!/bin/bash

echo "启动盐碱地土壤质量评价系统..."
echo

echo "1. 请确保MySQL服务已启动"
echo "2. 请确保已创建数据库并导入表结构"
echo "3. 请确保Java环境已配置"
echo

# 检查Java环境
if ! command -v java &> /dev/null; then
    echo "错误: 未找到Java环境，请先安装JDK"
    exit 1
fi

# 检查Maven环境
if ! command -v mvn &> /dev/null; then
    echo "错误: 未找到Maven环境，请先安装Maven"
    exit 1
fi

# 检查Node.js环境
if ! command -v node &> /dev/null; then
    echo "错误: 未找到Node.js环境，请先安装Node.js"
    exit 1
fi

echo "启动后端服务..."
cd saline-alkali-soil-master
nohup mvn spring-boot:run > ../backend.log 2>&1 &
BACKEND_PID=$!
cd ..

echo "后端服务已启动，PID: $BACKEND_PID"
echo "等待后端服务启动..."
sleep 10

echo "启动前端服务..."
nohup npm run serve > frontend.log 2>&1 &
FRONTEND_PID=$!

echo "前端服务已启动，PID: $FRONTEND_PID"
echo
echo "系统启动完成！"
echo "前端地址: http://localhost:3000"
echo "后端地址: http://localhost:8081"
echo
echo "日志文件:"
echo "  后端日志: backend.log"
echo "  前端日志: frontend.log"
echo
echo "停止服务:"
echo "  kill $BACKEND_PID $FRONTEND_PID"
echo

# 保存PID到文件
echo $BACKEND_PID > backend.pid
echo $FRONTEND_PID > frontend.pid

echo "按 Ctrl+C 停止服务"
trap 'echo "正在停止服务..."; kill $BACKEND_PID $FRONTEND_PID 2>/dev/null; rm -f backend.pid frontend.pid; exit' INT

# 等待用户中断
wait
